<?php
/**
* 宝箱系统
 */
class Treasure extends APIBase {
		
	public $tag = "Treasure";
		
	public function before() {
		$this->initMysql();
		return true;
	}
		
	public function logic() {
		
		if (!isset($this->param['vid'])) {
				//$this->returnError(310, "user must be needed");
				$vid = 0;
		}else{
				$vid = $this->param['vid'];
		}
		
		$now = time();
		$uid = $this->uid;
		//echo $uid;
		$lua_stat = <<<"EOT"
local i = redis.call('HGET', 'hu:$uid', 'win_count') + 0
if i >= 1 then
	redis.call('HSET', 'hu:$uid', 'win_count', 0)
end
return i
EOT;
		$n = $this->data_redis->eval($lua_stat);

//add begin
$lua_stats = <<<"EOT"
local m = redis.call('HGET', 'hu:$uid', 'money') + 0
return m
EOT;
		$money_test = $this->data_redis->eval($lua_stats);
//add end

		if ($money_test < Game::$win_reward_least_money){
			$this->returnError(301, 'your money is not fit');
		}

		if ($n == 0){
			$this->returnError(301, 'you are not the winest');
		}else{
			$index = $this->get_win_award();
			$type_value = rand(1,50);
			if($type_value < 50){
				$award_win = Game::$award_money;
				$type = 0;	
			}else{
				$award_win = Game::$award_coin;
				$type = 1;
			}
			$reward = $award_win[$index];
			
			//insert into record into mysql begin
			$player = $this->mysql->find("select * from player where id = $uid");
			if (count($player) <= 0){
					$this->returnError(312, 'user error.');	
			} 
			$record = array();
			$now = time();
			$record["uid"] =  $uid;
			$record["name"] =  $player[0]['name'];
			if ($type == 0){
					$record["money"] =  $reward;
					$record["coin"] =  0;
			}else{
					$record["money"] = 0;
					$record["coin"] = $reward;	
			}
			/*$record["create_time"] = $now;
			$ids = $this->mysql->insert("treauserecord", $record);
			if ($ids <= 0 ){
					$this->e("insert treauserecord error");
					$this->returnError(500, "server is error");	
			}*/
  			//insert into record into mysql end
 			
			if($type == 0){
				$money = $this->data_redis->hincrBy("hu:{$uid}", "money", $reward);
				$this->returnData(array('money' => $money, 'reward' => $reward, 'type' => $type));
			}else{
				$coin = $this->data_redis->hincrBy("hu:{$uid}", "coin", $reward);
				$coins = $this->data_redis->hincrBy("hu:{$uid}", "win_reward_coin", $reward);
				$this->returnData(array('coin' => $coin, 'reward' => $reward, 'type' => $type));
			}
		}									
	}
		
	private function get_win_award() {
		$configArr = array(0.6, 0.2, 0.1, 0.045, 0.03, 0.02,
                                   0.002, 0.0015, 0.001, 0.0005);
        	$arr = array();
        	$key = md5(serialize($configArr));
        	if (!isset($arr[$key])){
                	$max = array_sum($configArr);
                	foreach ($configArr as $k => $v){
                        	$v = $v / $max * 10000;
                        	for ($i=0; $i<$v; $i++){
                                	$arr[$key][] = $k;
                        	}
                	}
        	}
        	return $arr[$key][mt_rand(0,count($arr[$key])-1)];
	}
    		
	public function after() {
			$this->deinitDataRedis();
			$this->deinitMysql();
	}
}
?>
